
#include "StdAfx.h"
#include "LinkInputNode.h"
#include "CommonDef.h"
#include "LinkEdit.h"
#include "LinkComboBox.h"
#include "LinkButton.h"
#include "LinkDate.h"

CLinkInputNode* CLinkInputNode::m_spLinkHead = NULL;
CLinkInputNode* CLinkInputNode::m_spLinkTail = NULL;

CLinkInputNode::CLinkInputNode()
{
	ZeroMemory(this, sizeof(*this));
	
	m_dwType		= IBT_NoneType;
	m_pInputCtrl	= NULL;
	m_pPrev			= NULL;
	m_pNext			= NULL;
}

CLinkInputNode::~CLinkInputNode()
{

}

CLinkInputNode* CLinkInputNode::GetHeadNode()
{
	return m_spLinkHead;
}

CLinkInputNode* CLinkInputNode::GetPrevNode()
{
	return m_pPrev;
}

CLinkInputNode* CLinkInputNode::GetNextNode()
{
	return m_pNext;
}

CWnd* CLinkInputNode::GetCtrl()
{
	return m_pInputCtrl;
}

DWORD CLinkInputNode::GetCtrlType()
{
	return m_dwType;
}

BOOL CLinkInputNode::AddInput(CWnd* pInput, DWORD dwType)
{
	CLinkInputNode* pNewInput = new CLinkInputNode();
	if(pNewInput)
	{
		pNewInput->m_dwType		= dwType;
		pNewInput->m_pInputCtrl	= pInput;
		
		if(m_spLinkHead == NULL)
		{
			m_spLinkHead = pNewInput;
			m_spLinkTail = pNewInput;
		}
		else
		{
			m_spLinkTail->m_pNext	= pNewInput;
			pNewInput->m_pPrev		= m_spLinkTail;
			m_spLinkTail			= pNewInput;
		}

		return TRUE;
	}

	return FALSE;
}

BOOL CLinkInputNode::DelInput(CWnd* pInput)
{
	for(CLinkInputNode* pNode = m_spLinkHead; pNode; pNode = pNode->GetNextNode())
	{
		if(pNode->GetCtrl() == pInput)
		{
			CLinkInputNode* pPrev = pNode->GetPrevNode();
			if(pPrev)
				pPrev->m_pNext = pNode->m_pNext;
			
			CLinkInputNode* pNext = pNode->GetNextNode();
			if(pNext)
				pNext->m_pPrev = pNode->m_pPrev;

			if(m_spLinkHead == pNode)
				m_spLinkHead = pNode->m_pNext;

			if(m_spLinkTail == pNode)
				m_spLinkTail = pNode->m_pPrev;

			SAFE_DELETE(pNode);
			return TRUE;
		}
	}
	
	return FALSE;
}

BOOL CLinkInputNode::EnableInputs(BOOL bEnable, DWORD dwTypeMask, CWnd* pParent)
{
	for(CLinkInputNode* pNode = m_spLinkHead; pNode; pNode = pNode->GetNextNode())
	{
		DWORD dwType = pNode->GetCtrlType();
		if(dwType & dwTypeMask)
		{
			CWnd* pCtrl = pNode->GetCtrl();
			if(pParent == NULL || pCtrl->GetParent() == pParent)
				pCtrl->EnableWindow(bEnable);
		}
	}
	
	return TRUE;
}

BOOL CLinkInputNode::EnableCheckValid(BOOL bEnable, DWORD dwCtrlType, CWnd* pParent)
{
	for(CLinkInputNode* pNode = m_spLinkHead; pNode; pNode = pNode->GetNextNode())
	{
		CWnd* pCtrl = pNode->GetCtrl();
		if(pCtrl)
		{
			if(pNode->GetCtrlType() == IBT_EditBox)
			{
				CLinkEdit* pLinkEdit = (CLinkEdit*)pCtrl;
				if(pParent == NULL || pLinkEdit->GetParent() == pParent)
					pLinkEdit->EnableCustMsgProc(bEnable);
			}

			if(pNode->GetCtrlType() == IBT_EditDate)
			{
				CLinkDateEdit* pLinkDate = (CLinkDateEdit*)pCtrl;
				if(pParent == NULL || pLinkDate->GetParent() == pParent)
					pLinkDate->EnableCustMsgProc(bEnable);
			}
			
			if(pNode->GetCtrlType() == IBT_ComboBox)
			{
				CLinkComboBox* pLinkCombo = (CLinkComboBox*)pCtrl;
				if(pParent == NULL || pLinkCombo->GetParent() == pParent)
					pLinkCombo->EnableCustMsgProc(bEnable);
			}
		}
	}
	
	return TRUE;
}

